package org.yohailong.studyreactfrombilibili.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.yohailong.studyreactfrombilibili.dao.CommentDao;
import org.yohailong.studyreactfrombilibili.dao.UserDao;
import org.yohailong.studyreactfrombilibili.dao.entity.CommentEntity;
import org.yohailong.studyreactfrombilibili.dao.entity.UserEntity;
import org.yohailong.studyreactfrombilibili.pojo.req.PublishCommentReq;
import org.yohailong.studyreactfrombilibili.pojo.vo.CommentVO;
import org.yohailong.studyreactfrombilibili.pojo.vo.UserVO;
import org.yohailong.studyreactfrombilibili.service.CommentService;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 朱海龙
 * 2025/9/7 14:40
 */
@Slf4j
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private UserDao userDao;

    @Override
    public List<CommentVO> commentList() {
        List<CommentEntity> list = commentDao.list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<String> userIdList = list.stream()
                                      .map(CommentEntity::getUserId)
                                      .collect(Collectors.toList());
        List<UserEntity> userList = userDao.lambdaQuery()
                                           .in(UserEntity::getId, userIdList)
                                           .list();
        Map<String, UserVO> userVoMap = userList.stream()
                                                .map(this::convert)
                                                .collect(Collectors.toMap(UserVO::getUid, o -> o));
        return list.stream()
                   .map(commentEntity -> {
                       CommentVO commentVO = new CommentVO();
                       commentVO.setRpid(commentEntity.getId());
                       commentVO.setUser(userVoMap.get(commentEntity.getUserId()));
                       commentVO.setContent(commentEntity.getContent());
                       commentVO.setCtime(commentEntity.getCreateTime());
                       commentVO.setLike(commentEntity.getLikeCount());
                       return commentVO;
                   })
                   .collect(Collectors.toList());

    }

    @Override
    public Boolean publishComment(PublishCommentReq req) {
        CommentEntity commentEntity = new CommentEntity();
        commentEntity.setUserId(req.getUserId());
        commentEntity.setContent(req.getContent());
        return commentDao.save(commentEntity);
    }

    private UserVO convert(UserEntity userEntity) {
        UserVO userVO = new UserVO();
        userVO.setUid(userEntity.getId());
        userVO.setAvatar(userEntity.getAvatar());
        userVO.setUname(userEntity.getUname());
        return userVO;
    }
}
